package com.example.smartcity.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity.R
import com.example.smartcity.app.App
import com.example.smartcity.http.SellerBean

/**
 * @Author: Tailyou
 * @CreateDate: 2021/10/7 19:11
 * @Email: tailyou@163.com
 * @Description: RecycleView数据适配器（店铺列表）
 */
class SellerGridAdapter(private var datas: List<SellerBean>, var listener: OnItemClickListener) :
    RecyclerView.Adapter<SellerGridAdapter.MViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MViewHolder {
        var view =
            LayoutInflater.from(parent.context).inflate(R.layout.layout_item_grid_seller, null)
        return MViewHolder(view)
    }

    override fun onBindViewHolder(holder: MViewHolder, position: Int) {
        holder.sellerName.text = datas[position].name
        holder.score.text = "${datas[position].score}"
        holder.sale3Hour.text = "近3小时销${datas[position].saleNum3hour}"
        App.fetchRCImage(datas[position].imgUrl, holder.sellerPic, 8, R.mipmap.ic_more_service)
        holder.itemView.setOnClickListener {
            listener.onClick(position)
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    class MViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var sellerPic: ImageView = itemView.findViewById(R.id.iv_seller_pic)
        var sellerName: TextView = itemView.findViewById(R.id.tv_seller_name)
        var score: TextView = itemView.findViewById(R.id.tv_score)
        var sale3Hour: TextView = itemView.findViewById(R.id.tv_sale_3_hour)
    }

    interface OnItemClickListener {
        fun onClick(position: Int)
    }

}